<template>
    <div>
        <el-main>
            <!-- 查询表单 -->
            <el-form :inline="true" v-model="infoModel" size="small">
                <el-form-item label="厂商名称">
                    <el-select v-model="infoModel.makerId" placeholder="请选择" @change="handleMakerChange">
                        <el-option v-for="item in makerList" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="品牌名称">
                    <el-select v-model="infoModel.brandId" placeholder="请选择">
                        <el-option v-for="item in brandList" :key="item.id" :label="item.brandName"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车牌号码">
                    <el-input v-model="infoModel.autoNum" placeholder="请输入车牌号码"></el-input>
                </el-form-item>
                <el-form-item label="车辆类型">
                    <el-select v-model="infoModel.infoType" placeholder="请选择">
                        <el-option label="燃油车" value="0"></el-option>
                        <el-option label="电动车" value="1"></el-option>
                        <el-option label="混动车" value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车辆状态">
                    <el-select v-model="infoModel.status" placeholder="请选择">
                        <el-option label="未租" value="0"></el-option>
                        <el-option label="已租" value="1"></el-option>
                        <el-option label="维保" value="2"></el-option>
                        <el-option label="自用" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="租金下限">
                    <el-input v-model="infoModel.lowRent" placeholder="请输入租金下限"></el-input>
                </el-form-item>
                <el-form-item label="租金上限">
                    <el-input v-model="infoModel.highRent" placeholder="请输入租金上限"></el-input>
                </el-form-item>
                <el-form-item label="上牌日期">
                    <el-date-picker v-model="infoModel.registrationDataVal" type="daterange" align="right" unlink-panels
                        range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                        :picker-options="pickerOptions" value-format="yyyy-MM-dd"> 
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
                    <el-button type="warning" icon="el-icon-refresh" @click="resetForm">重置</el-button>
                    <el-button type="success" icon="el-icon-plus" @click="handleAdd">新增</el-button>
                    <el-button type="danger" icon="el-icon-delete" @click="handleDeleteBatch">批量删除</el-button>
                </el-form-item>
            </el-form>
            <el-divider></el-divider>
            <!-- 数据表格 -->
            <el-table border :data="tableData" stripe @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="40"></el-table-column>
                <el-table-column label="序号" width="50">
                    <template slot-scope="scope">
                        {{(start-1)*size+scope.$index + 1}}
                    </template>
                </el-table-column>
                <el-table-column prop="makerName" label="厂商名称" width="80px"></el-table-column>
                <el-table-column prop="brandName" label="品牌名称" width="80"></el-table-column>
                <el-table-column prop="autoNum" label="车牌号码" width="90"></el-table-column>
                <el-table-column prop="infoType" label="车辆类型" width="80">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.infoType==0" type="success">燃油车</el-tag>
                        <el-tag v-else-if="scope.row.infoType==1" type="success">电动车</el-tag>
                        <el-tag v-else-if="scope.row.infoType==2" type="success">混动车</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="color" label="颜色" width="75">
                    <template slot-scope="scope">
                        <span :style="{background:scope.row.color,height:'20px',width:'30px',display:'inline-block'}"></span>
                    </template>
                </el-table-column>
                <el-table-column prop="displacement" label="排量" width="80">
                    <template slot-scope="scope">
                        {{scope.row.displacement+scope.row.unit}}
                    </template>
                </el-table-column>
                <el-table-column prop="mileage" label="里程" width="80"></el-table-column>
                <el-table-column prop="rent" label="租金" width="80"></el-table-column>
                <el-table-column prop="registration" label="上牌日期"></el-table-column>
                <el-table-column prop="pic" label="图片" width="120">
                    <template slot-scope="scope">
                        <img :src="scope.row.pic" style="width: 60px;height: 50px;">
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="车辆状态" width="100">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.status==0" type="success">未租</el-tag>
                        <el-tag v-else-if="scope.row.status==1" type="success">已租</el-tag>
                        <el-tag v-else-if="scope.row.status==2" type="success">维保</el-tag>
                        <el-tag v-else-if="scope.row.status==3" type="success">自用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handlerUpdate(scope.row)" icon="el-icon-edit">修改</el-button>
                        <el-button type="danger" size="mini" @click="handlerDelete(scope.row)" icon="el-icon-delete">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页组件-->
            <el-pagination background layout="total,prev, pager, next,jumper" :current-page="start" :page-size="size"
                :total="total" @current-change="search">
            </el-pagination>
            <!-- 新增与修改的对话框 -->
            <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible" width="30%">
                <el-form :model="saveModel" label-width="80px" :rules="rules" ref="form">
                    <el-form-item label="所属厂商" prop="makerId">
                        <el-select v-model="saveModel.makerId" placeholder="请选择厂商" @change="selectMaker">
                            <el-option v-for="item in makerList" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="品牌" prop="brandId">
                        <el-select v-model="saveModel.brandId" placeholder="请选择品牌">
                            <el-option v-for="item in saveBrandList" :key="item.id" :label="item.brandName"
                                :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="车牌号码" prop="autoNum">
                        <el-input v-model="saveModel.autoNum" placeholder="请输入车牌号码"></el-input>
                    </el-form-item>
                    <el-form-item label="车辆类型" prop="infoType">
                        <el-radio-group v-model="saveModel.infoType">
                            <el-radio-button label="0">燃油车</el-radio-button>
                            <el-radio-button label="1">电动车</el-radio-button>
                            <el-radio-button label="2">混动车</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="汽车颜色" prop="color">
                        <el-color-picker v-model="saveModel.color"></el-color-picker>
                    </el-form-item>
                    <el-form-item label="汽车排量" prop="displacement">
                        <el-input-number v-model="saveModel.displacement" :step="0.1" :min="0" ></el-input-number>
                    </el-form-item>
                    <el-form-item label="排量单位" prop="unit">
                        <el-radio-group v-model="saveModel.unit">
                            <el-radio-button label="L">L</el-radio-button>
                            <el-radio-button label="T">T</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="行驶里程" prop="mileage">
                        <el-input-number v-model="saveModel.mileage" placeholder="请输入行驶里程"></el-input-number>
                    </el-form-item>
                    <el-form-item label="日租金额" prop="rent">
                        <el-input-number v-model="saveModel.rent" placeholder="请输入日租金额"></el-input-number>
                    </el-form-item>
                    <el-form-item label="车辆押金" prop="deposit">
                        <el-input-number v-model="saveModel.deposit" placeholder="请输入车辆押金"></el-input-number>
                    </el-form-item>
                    <el-form-item label="上牌日期" prop="registration">
                        <el-date-picker v-model="saveModel.registration" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"> </el-date-picker>
                    </el-form-item>
                    <el-form-item label="车辆状态" prop="status">
                        <el-radio-group v-model="saveModel.status">
                            <el-radio-button label="0">未租</el-radio-button>
                            <el-radio-button label="1">已租</el-radio-button>
                            <el-radio-button label="2">维保</el-radio-button>
                           <el-radio-button label="3">自用</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="车辆图片" prop="pic">
                        <el-upload class="avatar-uploader" :action="uploadPath"
                                :show-file-list="false" :on-success="handleavatarSuccess"
                                :before-upload="beforeavatarUpload" :data="uploadToken">
                                <img v-if="saveModel.pic" :src="saveModel.pic" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确 定</el-button>
                </div>
            </el-dialog>
        </el-main>
    </div>
</template>
<script>
import infoUtil from '@/api/auto_info'
import makerUtil from '@/api/auto_maker'
import brandUtil from '@/api/auto_brand'
import { getToken } from '@/utils/auth'
export default {
    data() {
        return {
            infoModel: {},
            makerList: [],
            brandList: [],
            saveBrandList: [],
            tableData: [],
            start: 1,
            size: 5,
            total: 0,
            multipleSelection: [],
            //日期控件
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            uploadPath: process.env.VUE_APP_BASE_API+'rental/oss/upload',
            uploadToken: { token: getToken() },
            titleMap:{
                create: '新增汽车',
                update: '修改汽车'
            },
            saveModel: {
            },
            dialogFormVisible: false,
            dialogStatus: 'create',
            rules: {
            },


        }
    },
    created() {
        this.search();
        this.searchMakerList();
    },
    methods: {
        async search(start=1){
            this.start = start;
            const res = await infoUtil.search(this.start,this.size,this.infoModel);
            if(res.success){
                this.tableData = res.data.records;
                this.total = res.data.total;
            }
        },
        searchMakerList(){
            makerUtil.selectAll().then(res=>{
                if(res.success){
                    this.makerList = res.data;
                }
            })
        },
        handleMakerChange(val) {
            brandUtil.selectByMakerId(val).then(res=>{
                if(res.success){
                    console.log(res.data);
                    this.brandList = res.data;
                }
            })
        },
        handleSearch(){
            if(this.infoModel.registrationDataVal!=undefined){
                //console.log(this.infoModel.registrationDataVal);
                this.infoModel.lowRegistrationDate = this.infoModel.registrationDataVal[0];
                this.infoModel.highRegistrationDate = this.infoModel.registrationDataVal[1];
            }
            this.search();
        },
        resetForm(){
            this.tableData = [];
            this.infoModel = {};
            this.search();
        },
        handleAdd(){
            this.dialogStatus = 'create';
            this.dialogFormVisible = true;
            this.saveModel = {};
            this.$nextTick(() => {
                this.$refs['form'].clearValidate();
            });
        },
        handleDeleteBatch(){
            if(this.multipleSelection.length==0){
                this.$message({
                    type: 'warning',
                    message: '请选择要删除的数据!'
                })
                return;
            }else{
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    infoUtil.delete(this.multipleSelection).then(res => {
                        if (res.success) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.search();
                        }
                    })
                })
            }
        },
        handlerUpdate(row){
            this.dialogStatus = 'update';
            this.dialogFormVisible = true;
            this.saveModel = row;
            brandUtil.selectByMakerId(row.makerId).then(res=>{
                if(res.success){
                    this.saveBrandList = res.data;
                }
            })
            this.$nextTick(() => {
                this.$refs['form'].clearValidate();
            });
        },
        handlerDelete(row){
            this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                infoUtil.delete(row.id).then(res => {
                    if (res.success) {
                        this.$message({
                            type: 'success',
                           message: '删除成功!'
                        })
                        this.search();
                    }
                })
            })
        },
        handleSelectionChange(val){
            this.multipleSelection = [];
            val.forEach(element => {
                this.multipleSelection.push(element.id)
            });
        },
        handleavatarSuccess(res, file) {
            this.saveModel.pic = res.data;
            this.$forceUpdate();
         },
        beforeavatarUpload(file) {
            const isJPG = file.type === 'image/jpeg'|| 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG或者PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        selectMaker(val){
            brandUtil.selectByMakerId(val).then(res=>{
                if(res.success){
                    this.saveBrandList = res.data;
                }
            })
        },
        createData() {
            this.$refs['form'].validate(async valid => {
                if (valid) {
                    await infoUtil.add(this.saveModel).then(res=>{
                        if(res.success){
                            this.$message({
                                message: '添加成功',
                                type: 'success'
                            });
                            this.dialogFormVisible = false;
                            this.search();
                        }
                    })
                }
            });
        },
        updateData() {
            this.$refs['form'].validate(async valid => {
                if (valid) {
                    await infoUtil.update(this.saveModel).then(res=>{
                        if(res.success){
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            this.dialogFormVisible = false;
                            this.search();
                        }
                    })
                }
            })
        },
    }
}

</script>
<style>
.el-aside {
    background-color: #fff;
    border-right: 1px solid #d5d0d0;
    border-top: 2px solid #be6868;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>